using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace VoxelEngine.TerrainGenerator
{
	[Serializable]
	public class FlatLandTerrainGenerator : TerrainGenerator
	{
		[SerializeField]
		protected AnimationCurve _terrainCurve;

		public override IEnumerator CreateWorldFromSeed(World world)
		{
			_voxelProps = new Dictionary<VoxelProp.PropType, VoxelProp>
			{
				{
					VoxelProp.PropType.Tree,
					new TreeProp()
				}
			};
			_simplexNoise3D = new SimplexNoise3D(_seed);
			float heightMult = 50f;
			float heightOffSet = 10f;
			UnityEngine.Debug.Log("Creating World Data");
			for (int x = 0; x < world.WorldVisibleSizeX; x++)
			{
				for (int i = 0; i < world.WorldVisibleSizeZ; i++)
				{
					float num = _simplexNoise3D.CoherentNoise(x, 0f, i, 1, 50) * heightMult + heightOffSet;
					num *= _terrainCurve.Evaluate((float)x / (float)world.WorldVisibleSizeX) * _terrainCurve.Evaluate((float)i / (float)world.WorldVisibleSizeZ);
					num *= 2f;
					int num2 = Mathf.RoundToInt(num);
					if (num2 == 1)
					{
						num2 = 2;
					}
					for (int j = 0; j < world.WorldVisibleSizeY; j++)
					{
						CreateColumn(world, x, j, i, num2);
					}
				}
				if (x % 5 == 0)
				{
					yield return null;
				}
			}
		}

		public override void CreateColumn(World world, int x, int y, int z, int surfaceHeight)
		{
			if (y <= 0)
			{
				world[x, y, z] = new Block(BlockType.HardStone, x, y, z);
			}
			else if (y < surfaceHeight - 1)
			{
				world[x, y, z] = new Block(BlockType.Dirt, x, y, z);
			}
			else if (y == surfaceHeight - 1)
			{
				world[x, y, z] = new Block(BlockType.Grass, x, y, z);
				_voxelProps[VoxelProp.PropType.Tree].TryAdd(x, y, z);
			}
			else if (y == surfaceHeight)
			{
				world[x, y, z] = new Block(BlockType.Height, x, y, z);
			}
			else
			{
				world[x, y, z] = new Block(BlockType.Air, x, y, z)
				{
					Light = byte.MaxValue
				};
			}
		}
	}
}
